

const path = require('path')
function resolve(dir) {
  return path.join(__dirname, dir)
}
const TerserPlugin = require("terser-webpack-plugin")

const vConsolePlugin = require('vconsole-webpack-plugin')
let objectProject = {

  CST:{
    entry: 'src/modules/CST/CST.js',
    template: 'public/CST.html',
    filename: 'CST.html',
    chunks: ['chunk-vendors', 'chunk-common','CST']
  }

}
let page = {}
let projectname = process.argv[5] || '' // 获取执行哪个文件
if (process.env.NODE_ENV == 'development') {
  page = objectProject
} else {
  if(projectname) {
    page[projectname] = objectProject[projectname]
  }else{
    page = objectProject
  }
}

module.exports = {
  runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本
  publicPath: '',
  outputDir: 'dist'+projectname, //标识是打包哪个文件
  productionSourceMap: false, // 不在production环境使用SourceMap
  pluginOptions:{
    "style-resources-loader" :{
      preProcessor: "less",
      patterns:[
        path.resolve(__dirname,'src/assets/styles/global-variable.less')
      ]
    }
  },
  css: {
    loaderOptions: {
      less: {
        modifyVars: {
          // 或者可以通过 less 文件覆盖（文件路径为绝对路径）
          hack: `true; @import "${ path.resolve(__dirname,'src/assets/styles/theme.less') }";`
        },
      },
    },
  },
  lintOnSave: process.env.NODE_ENV !== 'production',
  configureWebpack: (config) => {
    // 入口文件
    // config.entry.app = ['babel-polyfill', './src/main.js']
    // 删除console插件
    var optimization = {
      minimizer: [
        new TerserPlugin({
          parallel:true,
          terserOptions: {
            compress: {
              // drop_console: true,
              drop_debugger: true
            },
            format:{
              comments: false,//删除注释
            }
          }
        })
      ],
    }
    // 只有打包生产环境才需要将console删除
    if (process.env.VUE_APP_build_type == 'production') {
      config.optimization = optimization;
    }

    //生产环境去掉vconsole调试器
    let envType = process.env.NODE_ENV != 'production'
    let pluginsDev = [
      new vConsolePlugin({
        filter: [],
        enable: false
      })
    ]

    config.plugins = [...config.plugins, ...pluginsDev]


  },
  // 允许对内部的 webpack 配置进行更细粒度的修改。
  chainWebpack: (config) => {
    // 命名
    config.resolve.alias
      .set('@', resolve('src'))
      .set('ASSET', resolve('src/assets'))
      .set('MODULES',resolve('src/modules'))
      .set('COMPONENTS', resolve('src/components'))
      .set('MIXINS', resolve('src/mixins'))
      .set('UTILS', resolve('src/utils'))
      .set('_VIEW', resolve('src/modules/CST/page'))
      .set('_SERVICES', resolve('src/modules/CST/services'))

    // 打包文件带hash
    config.output.filename('[name].[hash].js').end()

    // 为了补删除换行而加的配置
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        // modify the options...
        options.compilerOptions.preserveWhitespace = true
        return options
      })
  },
  pages: page,
  devServer: {// 跨域
    port: 9527, // 端口号
    open: true, // 配置自动启动浏览器
    proxy: {// 配置跨域处理 可以设置多个
      '/api': {
        target: 'https://ms.sysush.com/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      '/zly': {
        target: "http://192.168.37.6:40000",
        changeOrigin: true,
        pathRewrite: {
          '^/zly': ''
        }
      // },
      //'/hezhi': {
        // target: "http://192.168.37.52:40000",
        //changeOrigin: true,
        // pathRewrite: {
      //    '^/hezhi': ''
        // }
      },


      
      '/hezhi': {
        target: "http://192.168.37.52:40000",
        changeOrigin: true,
        pathRewrite: {
          '^/hezhi': ''
        }
      },



      '/cgb': {
        target: "http://192.168.37.105:40000",
        changeOrigin: true,
        pathRewrite: {
          '^/cgb': ''
        }
      },



      '/image': {
        target: "http://10.2.88.6:10100",
        changeOrigin: true,
        pathRewrite: {
          '^/image': '/api/image'
        }
      },
      '/disease': {
        target: "http://10.2.88.6:10100",
        changeOrigin: true,
        pathRewrite: {
          '^/disease': '/api/disease'
        }
      },
      '/tag': {
        target: "http://10.2.88.6:10100",
        changeOrigin: true,
        pathRewrite: {
          '^/tag': '/api/tag'
        }
      },
    }
  }
}
